package battle
{
	import battle.map.*;
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import mx.core.INavigatorContent;
	
	public class BattleRenderer
	{
		public static  const DEFAULT_BACKGROUD:uint = 0xFF000000;
		
		protected var screenWidth:int;
		protected var screenHeight:int;	
		
		protected var relTileCols:int;
		protected var relTileRows:int;
		protected var halfRelTileCols:int;
		protected var halfRelTileRows:int;
		protected var relTotalTiles:int;
		
		
		protected var map:Map;
		
		protected var gameObjects:Dictionary;
		protected var battlePlayer:BattlerPlayer;
	
		protected var _canvas:BitmapData;
		protected var backGroundColor:int;
		
		
		
		public function BattleRenderer( screenWidth:int, screenHeight:int, backGroundColor:int,  gameObjects:Dictionary, battlePlayer:BattlerPlayer )
		{
			this.screenWidth = screenWidth;
			this.screenHeight = screenHeight;
			
			relTileCols = int( Math.ceil( Number( screenWidth ) / Map.TILE_WIDTH ) ); 
			relTileRows = int( Math.ceil( Number( screenHeight ) / Map.TILE_HEIGHT ) );
			
			relTotalTiles = relTileCols * relTileRows;
			
			halfRelTileCols = relTileCols >> 1;
			halfRelTileRows = relTileRows >> 1;
			
			
			this.gameObjects = gameObjects;
			this.battlePlayer = battlePlayer;
			
			
			_canvas = new BitmapData( screenWidth, screenHeight, true, null );
			
			this.backGroundColor = backGroundColor;
			
		
		}
		
		public function setup( map:Map ):void
		{
			this.map = map;
		}
		
		
		public function draw( dt:int ):void
		{
			//清空背景
			_canvas.fillRect( new Rectangle( 0, 0, _canvas.width, _canvas.height ), backGroundColor );
			
			var beginCol:int = 0;
			
			if( battlePlayer != null )
			{
				beginCol = ( battlePlayer.pos.x / Map.TILE_WIDTH ) - halfRelTileCols;
			}
			
			if( beginCol + halfRelTileCols >= map.cols )
			{
				beginCol = map.cols - relTileCols;
			}
			
			if( beginCol < 0 ) 
			{
				beginCol = 0;
			}
			
			var endCol:int = beginCol + relTileCols;
			
			if( endCol >= map.cols )
			{
				endCol = map.cols - 1;
			}
			
			var beginRow:int = 0;
			
			if( battlePlayer != null )
			{
				beginRow = ( battlePlayer.pos.y / Map.TILE_HEIGHT ) - halfRelTileRows;
			}
			
			if( beginRow + halfRelTileRows >= map.rows )
			{
				beginRow = map.rows - relTileRows;
			}
			
			if( beginRow < 0 )
			{
				beginRow = 0;
			}
			
			var endRow:int = beginRow + relTileRows;
			
			if( endRow >= map.rows )
			{
				endRow = map.rows - 1;
			}
			
			
			var beginIndex:int = beginRow * map.cols + beginCol;
			
			//绘制各层地表,如果地表层数大于1,则最后1层留作遮罩层
			var floorLayerLen:int = map.layers.length > 1 ? map.layers.length - 1 : 1;
			
			
			var jump:int = map.cols - endCol + beginCol + 1;
			
			for( var i:int = beginIndex, l:int=0; l < relTotalTiles; l++ )
			{	
				var curCol:int = int( i % map.cols );
				
				var curRow:int = int( i / map.cols );
				
				var x:int = ( curCol - beginCol ) * Map.TILE_WIDTH;
				
				var y:int = ( curRow - beginRow ) * Map.TILE_HEIGHT;
				
				var pos:Point = new Point( x, y );
				
				for( var j:int = 0 ; j < floorLayerLen; j++ )
				{
					var tile:Tile = map.layers[j][i];
					
					if( tile != null )
					{
						_canvas.copyPixels( Tileset.getTilesetBitmap( tile.tilesetName ), tile.getRect(), pos, null,null, true );
					}
				}
				
				if( curCol == endCol - 1 )
				{	
					i += jump;
				}
				else
				{
					i++;
				}
			}
			
			var startX:int = beginCol * Map.TILE_WIDTH;
			var startY:int = beginRow * Map.TILE_HEIGHT;
			
			for( var key:String in gameObjects )
			{
				var gameObj:BaseObject = BaseObject( gameObjects[key] );
				
				var col:int = gameObj.pos.x / Map.TILE_WIDTH;
				
				var row:int = gameObj.pos.y / Map.TILE_HEIGHT;
				
				if( col < beginCol || col > endCol || row < beginRow || row > endRow )
				{
					continue;
				}
				
				var graphic:Graphic = gameObj.getGraphic();
				
				if( graphic )
				{
					var pos:Point = new Point( gameObj.pos.x - startX, gameObj.pos.y - startY );
					
					_canvas.copyPixels( graphic.bitmapData, graphic.rect, pos, null, null, true );
				}
				
			}
			
			
			
			//绘制遮罩层
			if( map.layerNum > 1 )
			{
				var layer:Vector.<Tile> = map.layers[map.layerNum - 1];
				
				for( var i:int = beginIndex, l:int=0; l < relTotalTiles; l++ )
				{
					
					var curCol:int = int( i % map.cols );
					
					var curRow:int = int( i / map.cols );
					
					var tile:Tile = layer[i];
					
					if( tile != null )
					{
						var x:int = ( curCol - beginCol ) * Map.TILE_WIDTH;
						
						var y:int = ( curRow - beginRow ) * Map.TILE_HEIGHT;
						
						var pos:Point = new Point( x, y );
				
						_canvas.copyPixels( Tileset.getTilesetBitmap( tile.tilesetName ), tile.getRect(), pos, null,null, true );
					}
					
					if( curCol == endCol - 1 )
					{
						i += jump;
					}
					else
					{
						i++;
					}
				}
				
			}
			
		}
		
		public function get canvas():BitmapData
		{
			return _canvas;
		}
		
	}
	
}